#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std; 

int window(int work);
int window2(int work);
int window3(int work);
int window4(int work);
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct people{
	int id;
	int arrivetimem;
	int arrivetimeh;
	int work;
	struct people *next;
};
int main(int argc, char** argv) {
	int alltime=0;
	int customer=0;
	int a,b;
	struct people *first=NULL,*c1;
	struct people *second=NULL,*c2;
	struct people *third=NULL,*c3;
	struct people *fourth=NULL,*c4;
	struct people *c,*d; 
	int w1=0,w2=0,w3=0,w4=0;
	struct people *y[4];
	while(1)
	{
	printf("1，系统自动初始化并开始模拟\n");
	printf("2，手动设置数据实现模拟\n请输入：\n");
	scanf("%d",&a);
	if(a==1)
	{
		break;
	}
	else if(a==2)
	{
		printf("1,qukuan"); 
		printf("请依次输入第1号顾客的到达时间和所需办理的业务（例：8 00 1  （8点到办理取款））");
		first=(struct people*)malloc(sizeof(struct people));
		first->id  =1;
		scanf("%d%d%d",&first->arrivetimeh ,&first->arrivetimem ,&first->work );
		d=c;
		for(int e=2;e<10000;e++)
		{
			printf("1，继续取号\n2，取号完成并开始模拟\n"); 
		    scanf("%d",&b);
		    if(b==1)
		   {
			printf("请输入第%d号顾客到达时间和业务",e);
			c=(struct people*)malloc(sizeof(struct people));
			c->id =e;
			scanf("%d%d%d",&c->arrivetimeh ,&c->arrivetimem ,&c->work );
			c->next =NULL; 
			d->next =c;
			d=c;
	     	}
	    	else if(b==2)
		   {
			break;
		   }
		   else
		   {
			printf("输入错误请重新输入：");
			e--;
	     	}	
		}	
		break;
	}
	else
	{
		printf("输入错误请重新输入：");
	}
	
	 } 
	//开始模拟
	c=first;
	///上午 
	for(int i=8;i<12;i++)
	{
		for(int j=0;j<60;j++)
		
		{
			if(c->next ==NULL)
						{
							break;
						}
						else
						{
							c=c->next ;
						}
	
					if(((c->arrivetimeh )*60+c->arrivetimem )<=(i*60+j))
				{
						if(w1<=0)
					{
						w1=window(c->work );
						y[0]=c ;
						c=c->next ;
					}
					else if(w2<=0)
					{
						w2=window(c->work );
						y[1]=c ;
						c=c->next ;
					}
					else if(w3<=0)
					{
						w3=window(c->work );
						y[2]=c;
						c=c->next ;
					}
					else if(w4<=0)
					{
						w4=window(c->work );
						y[3]=c ;
						c=c->next ;
					}
					else
					{
						w1--;
						w2--;
						w3--;
						w4--;
						if(w1==0)
						{
							printf("第%d号顾客再第1号窗口办理完业务\n",y[0]->id  );
							alltime=alltime+((i*60+j)-(y[0]->arrivetimeh *60+y[0]->arrivetimem ));
							customer++;
						}
						else if(w2==0)
						{
							printf("第%d号顾客再第2号窗口办理完业务\n",y[1]->id  );
							alltime=alltime+((i*60+j)-(y[1]->arrivetimeh *60+y[1]->arrivetimem ));
							customer++;
						}
						else if(w3==0)
						{
							printf("第%d号顾客再第3号窗口办理完业务\n",y[2]->id  );
							alltime=alltime+((i*60+j)-(y[2]->arrivetimeh *60+y[2]->arrivetimem ));
							customer++;
						}
						else if(w4==0)
						{
							printf("第%d号顾客再第4号窗口办理完业务\n",y[3]->id  );
							alltime=alltime+((i*60+j)-(y[3]->arrivetimeh *60+y[3]->arrivetimem ));
							customer++;
						}
					}
				}
					 
			else if(i>12)
			{
				break;
			}
		}
		if(c->next ==NULL)
						{
							break;
						}
						else
						{
							c=c->next ;
						}
	 }
	 
	 ////下午 
	
	for(int i=14;i<18;i++)
	{
		for(int j=0;j<60;j++)
		
		{
			if(c->next ==NULL)
						{
							break;
						}
						else
						{
							c=c->next ;
						}
	
					if(((c->arrivetimeh )*60+c->arrivetimem )<=(i*60+j))
				{
						if(w1<=0)
					{
						w1=window(c->work );
						y[0]=c ;
						c=c->next ;
					}
					else if(w2<=0)
					{
						w2=window(c->work );
						y[1]=c ;
						c=c->next ;
					}
					else if(w3<=0)
					{
						w3=window(c->work );
						y[2]=c;
						c=c->next ;
					}
					else if(w4<=0)
					{
						w4=window(c->work );
						y[3]=c ;
						c=c->next ;
					}
					else
					{
						w1--;
						w2--;
						w3--;
						w4--;
						if(w1==0)
						{
							printf("第%d号顾客再第1号窗口办理完业务\n",y[0]->id  );
							alltime=alltime+((i*60+j)-(y[0]->arrivetimeh *60+y[0]->arrivetimem ));
							customer++;
						}
						else if(w2==0)
						{
							printf("第%d号顾客再第2号窗口办理完业务\n",y[1]->id  );
							alltime=alltime+((i*60+j)-(y[1]->arrivetimeh *60+y[1]->arrivetimem ));
							customer++;
						}
						else if(w3==0)
						{
							printf("第%d号顾客再第3号窗口办理完业务\n",y[2]->id  );
							alltime=alltime+((i*60+j)-(y[2]->arrivetimeh *60+y[2]->arrivetimem ));
							customer++;
						}
						else if(w4==0)
						{
							printf("第%d号顾客再第4号窗口办理完业务\n",y[3]->id  );
							alltime=alltime+((i*60+j)-(y[3]->arrivetimeh *60+y[3]->arrivetimem ));
							customer++;
						}
					}
				}
					 
			else if(i>18)
			{
				break;
			}
		}
		if(c->next ==NULL)
						{
							break;
						}
						else
						{
							c=c->next ;
						}
	 }
	 
	 ////
	 
	return 0;
}

int window(int work)
{
	return work*10;
}

int window2(int work)
{
	return work*10;
}

int window3(int work)
{
	return work*10;
}

int window4(int work)
{
	return work*10;
}
